package jp.co.sapporobeer.sbTest03;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class SafeToast extends Toast {
	private static Toast sToast = null;
	private static View slayout =null;
	private static CharSequence sCharSeq = null;
	private static int sResouceId = 0;

	public SafeToast(Context context) {
		super(context);
	}
	
	public static void setLayout(View layout,int resourceid, CharSequence cs){
		slayout = layout;
		sResouceId = resourceid;
		sCharSeq = cs;

		sToast.show();
	}
		
	@Override
    public void show() {
		ImageView image = (ImageView) slayout.findViewById(R.id.image);
        TextView text = (TextView) slayout.findViewById(R.id.text);
        image.setImageResource(sResouceId);

		//Toast toast = new Toast(getApplicationContext());
		text.setText(sCharSeq);

		this.setView(slayout);
		this.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
		this.setDuration(Toast.LENGTH_LONG);
	
		SafeToast.setToast(this);
		super.show();
    }

	public static void setToast(Toast toast) {
        if (sToast != null)
            sToast.cancel();
        sToast = toast;
    }

    public static void cancelToast() {
        if (sToast != null)
            sToast.cancel();
  
        sToast = null;
		slayout = null;
		sResouceId = 0;
		sCharSeq = null;
    }	
}
